﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetTraining.BLL;
namespace AspNetTraining
{
    public partial class UserAccounts : System.Web.UI.Page
    {
        public UserAccount SessionLogin
        {
            get
            {
                return (UserAccount)Session["LoggedUser"];
            }
            set
            {
                Session["LoggedUser"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            UserAccountManager userManager = new UserAccountManager();
            if (!IsPostBack)
            {
                // xoa account
                if (Request["id"] != null)
                {
                    userManager.DeleteAccount(new Guid(Request["id"].ToString()));
                    Response.Redirect("~/UserAccounts.aspx");
                    return;
                }
                /// kiem tra login
                if (SessionLogin == null)
                {
                    Response.Redirect("~/Account/Login.aspx?returnUrl="+Request.Url.PathAndQuery);
                }
                else
                {                         
                    UserAccount loggedUser = userManager.GetUserByUserNamePassword(SessionLogin.UserName, SessionLogin.Password);
                    if (loggedUser != null)
                    {
                        SessionLogin = loggedUser;
                        UserAccountManager accounts = new UserAccountManager();
                        gvAccounts.DataSource = accounts.GetListAccount();
                        gvAccounts.DataBind();
                    }
                    else
                    {
                        Response.Redirect("~/Account/Login.aspx");
                    }
                }
            }        
        }
        protected String GetGroupUserByID(object ID)
        {
            GroupUserManager groupuser=new GroupUserManager();
            return groupuser.GetGroupUserNameByID(new Guid(ID.ToString())); 
        }
        protected String GetUserNameByID(object ID)
        {
            if(ID==null)
            return null;
            UserAccountManager user = new UserAccountManager();
            return user.GetUserNameByUserID(new Guid(ID.ToString()));
        }
        /// <summary>
        /// xoa nhieu account chon
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Unnamed_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvAccounts.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("chkDelete");
                if(chk.Checked)
                {
                    UserAccountManager user = new UserAccountManager();
                    Label lbl = (Label)row.FindControl("lblID");
                    user.DeleteAccountByUserName(lbl.Text);
                }
            }
            Response.Redirect("~/UserAccounts.aspx");
        }
       
    }
}